﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace odinshrine
{
    class SpriteManager
    {
        public bool sauter = false;
        public Texture2D texture;
        public Vector2 Position = Vector2.Zero;
        public Dictionary<string, SpriteAnimated> Animations =
            new Dictionary<string, SpriteAnimated>();
        public int FrameIndex = 0;
        public int FrameHauteur = 1;
        public int FramesMax;
        public int HauteurMax;
        private ForcesHandler force = new ForcesHandler();
        public string SpriteAnime;
        public SpriteEffects SpriteEffect = SpriteEffects.None;
        public float Rotation = 0f;
        public Vector2 Origin = Vector2.Zero;
        public int height;
        public int width;
        public float scale = 1f;
        public bool Changeable;

        public SpriteAnimated spriteAnimed;
        public int s = 0;

        private float timeElapsed;
        public bool IsLooping = true;
        private float timeToUpdate = 0.10f;
        public float fpp;
        public float FpS
        {
            set
            {
                { timeToUpdate = 1f / value; }
                { fpp = value; }
            }
            get { return fpp; }
        }
        
        
        public SpriteManager()
        {
        }

        public SpriteManager(SpriteManager original)
        {
        this.texture = original.texture;
        this.Position = original.Position;
        this.Animations = original.Animations;
        this.FrameIndex = original.FrameIndex;
        this.FrameHauteur = original.FrameHauteur;
        this.FramesMax = original.FramesMax;
        this.HauteurMax = original.HauteurMax;
        this.SpriteAnime = original.SpriteAnime;
        this.SpriteEffect = original.SpriteEffect    ;
        this.Rotation = original.Rotation;
        this.Origin = original.Origin;
        this.height = original.height;
        this.width = original.width;
        this.scale = original.scale ;
        this.Changeable = original.Changeable;

        this.spriteAnimed = original.spriteAnimed;
        this.s = original.s;

        this.timeElapsed = original.timeElapsed;
        this.IsLooping = original.IsLooping;
        this.timeToUpdate = original.timeToUpdate;
        this.FpS = original.FpS;


        }

        public static SpriteManager Copy(SpriteManager original)
        {
            SpriteManager re;
            re = original;
            return (re);
        }

        public void AjoutAnimation(string name, int hauteur, int Frames)
        {

            spriteAnimed = new SpriteAnimated () ;
            Rectangle[] rectangles = new Rectangle[Frames];
            for (int i = 0; i < Frames; i++)
            {
                rectangles[i] = new Rectangle(i * width, (hauteur - 1) * height,
                    width, height);
            }

            spriteAnimed.Frames = Frames;
            spriteAnimed.Rectangles = rectangles;
            spriteAnimed.Hauteur = hauteur;
            Animations.Add(name,spriteAnimed);
            this.s++;

        }

        public void Load(Texture2D texture, int FramesMax, int HauteurMax)
        {
            this.texture = texture;
            this.FramesMax = FramesMax;
            this.HauteurMax = HauteurMax;
            this.height = texture.Height / HauteurMax;
            this.width = texture.Width  / FramesMax;
            this.SpriteAnime = "Idle";
        }

        public void ChangeAnime(string SpriteAnime, bool stat)
        {

            if (this.SpriteAnime != SpriteAnime && Changeable)
            {
                this.FrameIndex = 0 ;
                this.Changeable = stat;
                FrameHauteur = Animations[SpriteAnime].Hauteur;
            }
            this.SpriteAnime = SpriteAnime;
        }



        public void Update(GameTime gameTime)
        {
            force.Update(!sauter, gameTime);
      //      Position += force.Myspeed;
            timeElapsed += (float)
                gameTime.ElapsedGameTime.TotalSeconds;

            if (timeElapsed > timeToUpdate)
            {
                timeElapsed -= timeToUpdate;

                if (FrameIndex < Animations[SpriteAnime].Frames - 1)
                    FrameIndex++;
                else if (IsLooping)
                    FrameIndex = 0;
                    Changeable = true;

            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            
                spriteBatch.Draw(texture, new Rectangle((int)Position.X, (int)Position.Y, (int)(width * scale), (int)(height * scale)), Animations[SpriteAnime].Rectangles[FrameIndex],
                    Animations[SpriteAnime].Color, Rotation, Origin, SpriteEffect,
                    0f);
            
        }
    }
}
